import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:address_picker/address_picker.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_pickers/pickers.dart';

class ManageAddress extends StatefulWidget {
  ManageAddress(
      {Key key,
      this.id,
      this.removeAddress,
      this.province,
      this.addressIntro,
      this.district,
      this.municipality,
      this.name,
      this.phone})
      : super(key: key);
  int id;

  String name = '未收到请求';
  String phone = '13533333333';
  String province;
  String municipality;
  String district;
  String addressIntro;
  ValueChanged<int> removeAddress;

  TextEditingController _textNameValue = new TextEditingController(text: '');
  TextEditingController _textPhoneValue = new TextEditingController(text: '');
  TextEditingController _textAddressValue = new TextEditingController(text: '');

  @override
  _ManageAddressState createState() => _ManageAddressState();
}

class _ManageAddressState extends State<ManageAddress> {
  // String initProvince = widget.province, initCity = '成都市', initTown = '双流区';
  @override
  initState() {
    // TODO: implement initState
    super.initState();
    // 页面加载时调用 用来开始时访问数据库

    widget._textNameValue = new TextEditingController(text: widget.name);
    widget._textPhoneValue = new TextEditingController(text: widget.phone);
    widget._textAddressValue = new TextEditingController(
        text: widget.province +
            ' ' +
            widget.municipality +
            ' ' +
            widget.district +
            ' ' +
            widget.addressIntro);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // child: Text('123'),
      appBar: AppBar(
        centerTitle: true,
        title: Text('编辑收货地址'),
        backgroundColor: Colors.blue,
        actions: [
          InkWell(
            onTap: () {
              setState(() {});
            },
            child: InkWell(
              onTap: () async {
                var response = await Http.get(
                    'http://xiangrikui.vaiwan.com/userAddress/delete?addressId=${widget.id}');

                setState(() {
                  Navigator.pop(context);
                  widget.removeAddress(widget.id);
                });
              },
              child: Container(
                padding: EdgeInsets.all(18),
                child: Text(
                  '删除',
                  style: TextStyle(fontSize: 14),
                ),
              ),
            ),
          ),
        ],
      ),
      body: Container(
        color: Colors.black12,
        padding: const EdgeInsets.all(16.0),
        child: ListView(
          children: [
            Container(
                // height: 100,
                padding: EdgeInsets.only(top: 6),
                // color: Colors.red,
                child: Column(children: [
                  Container(
                    margin: EdgeInsets.only(bottom: 10),
                    child: Row(children: [
                      Container(
                          width: 80,
                          // color: Colors.yellow,
                          child: Text('收货人')),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
                        child: Container(
                          // color: Colors.black45,
                          width: 150,
                          height: 24,
                          child: TextField(
                            style: TextStyle(
                                fontSize: 16, color: Colors.grey[400]),
                            controller: widget._textNameValue,
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              contentPadding: EdgeInsets.all(6.0),
                              hintStyle: new TextStyle(
                                  fontSize: 16, color: Colors.grey[400]),
                              hintText: '请输入姓名',
                            ),
                            // onChanged: _textConsignee,
                            autofocus: false,
                          ),
                        ),
                      ),
                    ]),
                  ),
                  Divider(
                    height: 0,
                    color: Colors.black87,
                    indent: 0,
                    endIndent: 0,
                  ),
                  Container(
                    margin: EdgeInsets.only(bottom: 10, top: 10),
                    child: Row(children: [
                      Container(
                          width: 80,
                          // color: Colors.yellow,
                          child: Text('手机号')),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
                        child: Container(
                          // color: Colors.black45,
                          width: 150,
                          height: 24,
                          child: TextField(
                            style: TextStyle(
                                fontSize: 16, color: Colors.grey[400]),
                            controller: widget._textPhoneValue,
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              contentPadding: EdgeInsets.all(6.0),
                              hintStyle: new TextStyle(
                                  fontSize: 16, color: Colors.grey[400]),
                              hintText: '请输入手机号',
                            ),
                            // onChanged: _textConsignee,
                            autofocus: false,
                          ),
                        ),
                      ),
                    ]),
                  ),
                  Divider(
                    height: 0,
                    color: Colors.black87,
                    indent: 0,
                    endIndent: 0,
                  ),
                  Container(
                    margin: EdgeInsets.only(bottom: 10, top: 10),
                    child: Row(children: [
                      Container(
                          width: 80,
                          // color: Colors.yellow,
                          child: Text('选择地址')),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
                        child: Container(
                          // color: Colors.black45,
                          width: 150,
                          height: 24,
                          child: InkWell(
                            onTap: () {
                              Pickers.showAddressPicker(
                                context,
                                initProvince: widget.province,
                                initCity: widget.municipality,
                                initTown: widget.district,
                                onConfirm: (p, c, t) {
                                  setState(() {
                                    widget.province = p;
                                    widget.municipality = c;
                                    widget.district = t;
                                  });
                                },
                              );
                            },
                            child: Text(widget.province +
                                ' ' +
                                widget.municipality +
                                ' ' +
                                widget.district),
                          ),
                        ),
                      ),
                    ]),
                  ),
                  Container(
                    margin: EdgeInsets.only(bottom: 10, top: 10),
                    child: Row(children: [
                      Container(
                          width: 80,
                          // color: Colors.yellow,
                          child: Text('详细地址')),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
                        child: Container(
                          // color: Colors.black45,
                          width: 200,
                          height: 80,

                          child: TextField(
                            maxLength: 30,
                            maxLines: 3,
                            style: TextStyle(
                                fontSize: 16, color: Colors.grey[400]),
                            controller: widget._textAddressValue,
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              contentPadding: EdgeInsets.all(6.0),
                              hintStyle: new TextStyle(
                                  fontSize: 16, color: Colors.grey[400]),
                              hintText: '请输入详细地址',
                            ),
                            // onChanged: _textConsignee,
                            autofocus: false,
                          ),
                        ),
                      ),
                    ]),
                  ),
                  Divider(
                    height: 0,
                    color: Colors.black87,
                    indent: 0,
                    endIndent: 0,
                  ),
                  InkWell(
                    onTap: () async {
                      var response = await Http.get(
                          'http://xiangrikui.vaiwan.com/userAddress/updateById?id=${widget.id}&name=${widget._textNameValue.text}&phone=${widget._textPhoneValue.text}&province=${widget.province}&municipality=${widget.municipality}&district=${widget.district}&addressIntro=${widget._textAddressValue.text}');
                      setState(() {
                        Navigator.pop(context);
                        widget.removeAddress(widget.id);
                      });
                    },
                    child: Container(
                        margin: EdgeInsets.only(top: 30),
                        width: 280,
                        height: 34,
                        // color: Colors.red,
                        decoration: new BoxDecoration(
                            color: Colors.red,
                            borderRadius: new BorderRadius.circular((10.0)),
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                  blurRadius: 5.0, //阴影模糊程度
                                  spreadRadius: 0.1 //阴影扩散程度
                                  )
                            ]),
                        child: Center(
                            child: Text(
                          '保存',
                          style: TextStyle(color: Colors.white),
                        ))),
                  )
                ])),
          ],
        ),
      ),
    );
  }
}
